/****************************************************************************

The main algorithm is:
1) On filter hide all elements that do not match filter.
This can be done for as many columns as have filtering turned on.
2) On change filter hide all elements which do not match filter
and show all that do. Then reapply filters on all the other columns
3) On undo filter make all visible. Then reapply filters on all
the other columns
****************************************************************************/


//-----------------------------------------------------------------------------
//                     filter utility routines
//-----------------------------------------------------------------------------

function hideAll(tBody)
{
   for (i = 0;i < tBody.rows.length;i++)
   {
      tBody.rows[i].style.display = "none";
   }
}

function showAll(tBody)
{
   for (i = 0;i < tBody.rows.length;i++)
   {
      tBody.rows[i].style.display = "";
   }
}

function showAllNotfiltered(tBody, col, filter)
{
   for (i = 0;i < tBody.rows.length;i++)
   {
      var row = tBody.rows[i];
      // filter is already trimed so trim text
      var cellText = trim(getTextValue(row.cells[col]));;
      if (compareValues(filter, cellText) != 0)
      {
         row.style.display = "none";
      }
      else
      {
         row.style.display = "";
      }
   }
}

function filterTableUsingColumnFilter(tBody, col, filter)
{
   for (i = 0;i < tBody.rows.length;i++)
   {
      var row = tBody.rows[i];
      // filter is already trimed so trim text
      var cellText = trim(getTextValue(row.cells[col]));
      if (compareValues(filter, cellText) != 0)
      {
         row.style.display = "none";
      }
   }
}

function unsetFilters(tableId, e)
{
   // need to consume event so single click in not honored
   // after the first call stop propagation of event
   if (!e) e = window.event;
   e.cancelBubble = true;
   if (e.stopPropagation) e.stopPropagation();
   // Get the table section to filter
   var tBody = document.getElementById(tableId);
   var imgID;
   var imgObj;

   var numColumns = tBody.rows[0].cells.length;
   // show all rows
   showAll(tBody);
   // set all last selected to all (0)
   for (c = 0; c < numColumns;c++)
   {
      colObj = document.getElementById(tableId +  c);
      if (colObj != null)
      {
         imgID = tableId + datatableImageBase + col;
         imgObj = document.getElementById(imgID);
         imgObj.src=datatableFilterImage;
         colObj.lastSelectedIndex = 0;
      }
   }
}

function saveFilters(tBody)
{
   var tableId = tBody.id;
   var filteredColumns = document.getElementById(tableId + datatableFilteredColumns);
   var oldFilteredColumns = "";
   var numColumns = tBody.rows[0].cells.length;
   filteredColumns.value = "";
   for (c = 0; c < numColumns;c++)
   {
      var colObj = document.getElementById(tableId + c);
      if (colObj != null)
      {
         var lastSelectedIndex =  colObj.lastSelectedIndex;
         var filterString =  colObj.filterValue;
         if (lastSelectedIndex > 0)
         {
            filteredColumns.value = oldFilteredColumns + c +
                              datatableOnLoadSubDivider +
                              lastSelectedIndex +
                              datatableOnLoadSubDivider +
                              filterString;
            oldFilteredColumns = filteredColumns.value + datatableOnLoadDivider;
         }
      }
   }

}

//-----------------------------------------------------------------------------
// main Filter function
//-----------------------------------------------------------------------------
// Parameters:
//      - Table ID
//      - col to filter
//      - selected index
//      - string to filter on
//      - name of this menu
//
// Note that all the tables which are to be filtered must contain an ID tag.
// So, if they do not exist, you must create one for each table. Also, the
// table names/ids MUST BE UNIQUE.
function filterTable(tableId, col, selectedIndex, thisFilter, menuName)
{
   var tBody = document.getElementById(tableId);
   var oldDsply = tBody.style.display;

   doFilterTable(tBody, col, selectedIndex, thisFilter);
   // Set the table display style to "none" - necessary for Netscape 6
   // browsers.
   tBody.style.display = "none";

   // restore odd even colors
   setRowClass(tBody);

   // Restore the tables display style.
   tBody.style.display = oldDsply;
   
   setScrollProperty(tableId);
   
   mEvent(menuName, 0, 't')
}
// This is the function that actually does the filterinng.
// Notice the menu is not passed in.  This function is called both
// from filterTable and initTable which is called when the on load
// event is triggered and we do not have menue event is handled by
// caller if it is needed

function doFilterTable(tBody, col, selectedIndex, thisFilter)
{
   var tableId = tBody.id;
   // Get the table section to filter
   var thID = tableId + col;
   var thObj = document.getElementById(thID);
   var imgID = tableId + datatableFilterBase + datatableImageBase + col;
   var imgObj = document.getElementById(imgID);
   var lastSelectedIndex =  thObj.lastSelectedIndex;
   var trimmedFilter = trim(thisFilter);
   // if previously not filtered ...
   if (selectedIndex > 0 && lastSelectedIndex == 0)
   {
      // filter column
      filterTableUsingColumnFilter(tBody, col, trimmedFilter);

      // change image to indicate that this column is filtered
      thObj.lastSelectedIndex = selectedIndex
      thObj.filterValue = thisFilter
      imgObj.src=datatableFilteredImage;
   }
   else
   {
      thObj.lastSelectedIndex = selectedIndex
      thObj.filterValue = thisFilter
      // if undoing filtering for this column
      if (selectedIndex == 0)
      {
         // undo filters for this column
         showAll(tBody);
         // change image to indicate that this column is not filtered
         imgObj.src=datatableFilterImage;
      }
      else
         // filter column using new filter
         showAllNotfiltered(tBody, col, trimmedFilter);
      var numColumns = tBody.rows[0].cells.length;
      var colObj;
      for (c = 0; c < numColumns;c++)
      {
         if (c != col)
         {
            colObj = document.getElementById(tableId +  c);
            if (colObj != null)
            {
               var selIndex =  colObj.lastSelectedIndex;
               if (selIndex > 0)
               {
                  filterTableUsingColumnFilter(tBody, c, colObj.filterValue);
               }
            }
         }
      }
   }
   saveFilters(tBody);
}
